package com.example.websocket.service.impl;

import com.example.websocket.mapper.UserMapper;
import com.example.websocket.pojo.LoginDTO;
import com.example.websocket.pojo.Result;
import com.example.websocket.pojo.User;
import com.example.websocket.service.UserService;
import com.example.websocket.utils.AppJwtUtil;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ author 韩
 * time: 2024/2/16 18:04
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(LoginDTO dto, HttpSession session) {
        // 1.先校验参数
        if (StringUtils.isNotBlank(dto.getUsername()) && StringUtils.isNotBlank(dto.getPassword())) {
            User user = userMapper.selectByUserName(dto.getUsername());
            if (user == null) {
                return Result.error("不存在该用户名");
            }
            // 2.比对密码
            if (!dto.getPassword().equals(user.getPassword())) {
                return Result.error("密码错误");
            }
            // 3.把username放到httpsession里
            session.setAttribute("username",dto.getUsername());
            // 4.生成token 不要把敏感信息放在token里
            String token = AppJwtUtil.getJwtToken(user.getId(),user.getUsername());
            Map<String,Object> map = new HashMap<>();
            map.put("token",token);
            map.put("username",dto.getUsername());
            return Result.ok(map);
        }else {
            return Result.error("用户名或密码为空");
        }
    }

    @Override
    public Result register(LoginDTO dto) {
        // 验证密码长度
        if (!isValidPassword(dto.getPassword())) {
            return Result.error("用户名或密码必须由3~15字符组成！");
        }
        // 验证用户名长度
        if (!isValidUsername(dto.getUsername())) {
            return Result.error("用户名或密码必须由3~15个字符组成！");
        }

        User user = userMapper.selectByUserName(dto.getUsername());
        if (user == null) {
            userMapper.addUser(dto.getUsername(), dto.getPassword());
        } else {
            return Result.error("已存在该用户名");
        }
        return Result.ok();
    }



    // 验证密码长度的方法
    private boolean isValidPassword(String password) {
        // 密码长度范围为3~15个字符
        Pattern pattern = Pattern.compile("^\\S{3,15}$");
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }

    // 验证用户名长度的方法
    private boolean isValidUsername(String username) {
        // 用户名长度范围为3~10个字符
        Pattern pattern = Pattern.compile("^\\S{3,15}$");
        Matcher matcher = pattern.matcher(username);
        return matcher.matches();
    }

    @Override
    public List<String> getUserList() {
        List<String> userList = userMapper.getList();
        return userList;
    }
}
